<template>
  <div class="dashboard-container" >
    <h1 style="text-align: center;margin-bottom: 40px;font-size: 35px;color: #486180">顾客管理</h1>
    <el-dialog title="新增" :visible.sync="addButtonController" width="30%" @close="closeCustomer('ruleForm')">
      <el-form :rules="roles" label-width="20%" :model="addList" ref="ruleForm">
        <el-form-item label="顾客名称" prop="customerName">
          <el-input v-model="addList.customerName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入顾客名称"/>
        </el-form-item>
        <el-form-item label="性别"  prop="sex">
          <el-radio-group v-model="addList.sex">
            <el-radio label="男" value="男">男</el-radio>
            <el-radio label="女" value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="addList.phoneNumber" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入电话号码"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="identificationCard">
          <el-input v-model="addList.identificationCard" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="info" plain @click="addCustomer('ruleForm')">确 定</el-button>
    <el-button plain @click="closeCustomer('ruleForm')">取 消</el-button>
  </span>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="updateButtonController" width="30%" @close="closeCustomer('ruleForm')">
      <el-form :rules="roles" label-width="20%" :model="updateList" ref="ruleForm">
        <el-form-item label="顾客名称" prop="customerName">
          <el-input v-model="updateList.customerName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入顾客名称"/>
        </el-form-item>
        <el-form-item label="性别"  prop="sex">
          <el-radio-group v-model="updateList.sex">
            <el-radio label="男" value="男">男</el-radio>
            <el-radio label="女" value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="updateList.phoneNumber" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入电话号码"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="identificationCard">
          <el-input v-model="updateList.identificationCard" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="updateCustomer('ruleForm')">确 定</el-button>
        <el-button plain @click="closeCustomer('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="删除" :visible.sync="deleteButtonController" width="30%" @close="closeDelete('ruleForm')">
      <div style="margin: 10px"><span>请确认是否删除</span></div>
      <span slot="footer" class="dialog-footer">
          <el-button type="info" plain @click="deleteCustomer('ruleForm')">确定</el-button>
          <el-button plain @click="closeDelete('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:10% ;" @click="addButton">新增</el-button>
    <el-input
      v-model="customerName"
      placeholder="顾客名称"
      name="customerName"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-input
      v-model="phoneNumber"
      placeholder="电话号码"
      name="phoneNumber"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-input
      v-model="identificationCard"
      placeholder="身份证号"
      name="identificationCard"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:40px;" @click="selectCustomer">查询</el-button>
    <el-button  style="width:80px;margin-bottom:30px;margin-left:40px;" @click="checkCustomer">重置</el-button>
    <el-table :data="customerList" height="659px" border style="width: 100%;margin-top: 20px">
      <el-table-column align="center" fixed prop="customerId" label="顾客ID"/>
      <el-table-column align="center" fixed prop="customerName" label="顾客名称"/>
      <el-table-column align="center" fixed prop="sex" label="性别"/>
      <el-table-column align="center" fixed prop="phoneNumber" label="电话号码"/>
      <el-table-column align="center" fixed prop="identificationCard" label="身份证号"/>
      <el-table-column align="center" fixed prop="remark" label="备注"/>
      <el-table-column align="center" fixed label="操作">
        <template v-slot="scope">
          <!--  修改 -->
          <el-button class="el-icon-edit" circle @click="updateButton(scope.row)"></el-button>
          <el-button class="el-icon-delete" circle @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteCustomer, insertCustomer, selectAllCustomer, updateCustomer } from '@/api/Customer'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data(){
    return{
      customerName:'',
      phoneNumber:'',
      identificationCard:'',
      customerList:[],
      addButtonController:false,
      updateButtonController:false,
      deleteButtonController:false,
      deleteCustomerId:'',
      deleteCustomerTableId:'',
      addList:{
        customerName:'',
        sex:'',
        phoneNumber:'',
        identificationCard:'',
        remark:'',
      },
      updateList:{
        customerTableId:'',
        customerId:'',
        customerName:'',
        sex:'',
        phoneNumber:'',
        identificationCard:'',
        remark:'',
      },
      roles: {
        customerName: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { min: 1, max: 11, message: '长度在1到11个数字', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' }
        ],
        identificationCard: [
          { required: true, trigger: 'blur', message: '身份证号不能为空' },
          { min: 18, max: 18, message: '请输入18位身份证号', trigger: 'blur' }
        ],
        sex: [
          { required: true, trigger: 'blur', message: '性别不能为空' },
        ],
      }
    }
  },
  mounted() {
    this.selectCustomer()
  },
  methods:{
    selectCustomer(){
      selectAllCustomer({
        customerName:this.customerName,
        phoneNumber:this.phoneNumber,
        identificationCard:this.identificationCard
      }).then(
        response => {
          console.log(response.data)
          this.customerList = response.data
        }
      )
    },
    addButton(){
      this.addList = {
        customerName:'',
        sex:'',
        phoneNumber:'',
        identificationCard:'',
        remark:''
      }
      this.addButtonController = true
    },
    updateButton(row){
      this.updateList = {
        customerTableId:row.customerTableId,
        customerId:row.customerId,
        customerName:row.customerName,
        sex:row.sex,
        phoneNumber:row.phoneNumber,
        identificationCard:row.identificationCard,
        remark:row.remark
      }
      this.updateButtonController = true
    },
    deleteButton(row){
      this.deleteCustomerTableId = row.customerTableId
      this.deleteCustomerId = row.customerId
      this.deleteButtonController = true
    },
    addCustomer(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertCustomer({
            customerName:this.addList.customerName,
            sex:this.addList.sex,
            phoneNumber:this.addList.phoneNumber,
            identificationCard:this.addList.identificationCard,
            remark:this.addList.remark
          }).then(
            response => {
              this.addButtonController = false
              this.$message.success("新增成功！"); // 添加成功提示
              this.selectCustomer()
            }
          )
        } else {
          return false
        }
      })
    },
    checkCustomer(){
      this.customerName = ''
      this.phoneNumber = ''
      this.identificationCard = ''
      this.selectCustomer()
    },
    closeCustomer(formName){
      this.$refs[formName].resetFields();
      this.addButtonController = false;
      this.updateButtonController = false;
    },
    updateCustomer(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateCustomer({
            customerTableId:this.updateList.customerTableId,
            customerId:this.updateList.customerId,
            customerName:this.updateList.customerName,
            sex:this.updateList.sex,
            phoneNumber:this.updateList.phoneNumber,
            identificationCard:this.updateList.identificationCard,
            remark:this.updateList.remark
          }).then(
            async response => {
              this.updateButtonController = false
              this.selectCustomer()
            })
        } else {
          return false;
        }
      });
    },
    closeDelete(){
      this.addButtonController = false
      this.updateButtonController = false
      this.deleteButtonController = false
    },
    deleteCustomer(formName) {
      deleteCustomer({
        customerId:this.deleteCustomerId,
        customerTableId:this.deleteCustomerTableId
      }).then(
        async response => {
          this.deleteButtonController = false
          this.selectCustomer()
        })
    },

  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
